//
//  htvlogin_new.m
//  HTAppBeauty
//
//  Created by zhangwq on 15/8/1.
//  Copyright (c) 2015年 HT. All rights reserved.
//

#import "htvlogin_new.h"
#import "HTFrameKit.h"
#import "HTSysKit.h"
#import "NavBarBackgroundGradientView.h"
#import "AppDelegate.h"
#import "XYAlertViewHeader.h"
#import "DXAlertView.h"

@interface htvlogin_new (){
    BOOL isSeleted;
    MONActivityIndicatorView *indicatorView;
    NSTimer *timer;
    int success;
    UIWebView *webview;
    CGFloat width;
    CGFloat height;
    
    UIImageView *avatar;
    
    UITextField *inCompField;
    UITextField *inUserField;
    UITextField *inPasswordField;
    UIButton *loginButton;
}

@end

@implementation htvlogin_new




- (void)viewDidLoad {
    [super viewDidLoad];


    
    
    CGRect rectMain = [[UIScreen mainScreen] bounds];
    CGSize size = rectMain.size;
    width = size.width;
    height = size.height;
    
    
    //头像
    avatar=[[UIImageView alloc]initWithFrame:CGRectMake(width/2-50, 50, 100, 100)];
    avatar.image=[UIImage imageNamed:@"avatar.jpg"];
    avatar.layer.cornerRadius=50;
    [avatar setClipsToBounds:YES];
    [avatar setContentMode:UIViewContentModeScaleToFill];
    avatar.layer.masksToBounds=YES;
    [self.view addSubview:avatar];
    
    //用户名
    inUserField=[[UITextField alloc]initWithFrame:CGRectMake(0, 170, width, 40)];
    inUserField.backgroundColor=[UIColor whiteColor];
    inUserField.textAlignment=UITextAlignmentCenter;
    inUserField.font=[UIFont fontWithName:@"Arial" size:12.0];
    inUserField.placeholder=@"账 号";
    inUserField.delegate=self;
    [self.view addSubview:inUserField];
    
    //密码
    inPasswordField=[[UITextField alloc]initWithFrame:CGRectMake(0, 210.6, width, 40)];
    inPasswordField.backgroundColor=[UIColor whiteColor];
    inPasswordField.textAlignment=UITextAlignmentCenter;
    inPasswordField.placeholder=@"密 码";
    inPasswordField.font=[UIFont fontWithName:@"Arial" size:12.0];
    inPasswordField.secureTextEntry=YES;
    inPasswordField.delegate=self;
    [self.view addSubview:inPasswordField];
    
    //中间横线
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 210, width, 0.6)];
    line.backgroundColor=[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
    [self.view addSubview:line];
    
    
    //登录
    loginButton=[[UIButton alloc]initWithFrame:CGRectMake(width/2-120, 270,240 , 45)];
    loginButton.backgroundColor=[UIColor colorWithRed:91.0/255.0 green:138.0/255.0 blue:218.0/255.0 alpha:1.0];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginButton.font=[UIFont fontWithName:@"Arial" size:15.0];
    loginButton.layer.cornerRadius=4;
    [loginButton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    //去掉键盘
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearAction:)];
    [self.view addGestureRecognizer:singleTap];

    
    self.navigationController.navigationBar.hidden=YES;
    self.hidesBottomBarWhenPushed=YES;
    self.navigationController.navigationBar.translucent = NO;
    

    
    
    //读取缓存信息
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *username = [defaults objectForKey:@"username"];
    
    inUserField.text=username;

    

}



- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [inUserField resignFirstResponder];
    [inPasswordField resignFirstResponder];
    return YES;
}


-(void)clearAction:(UITapGestureRecognizer*)sender{
    [inUserField resignFirstResponder];
    [inPasswordField resignFirstResponder];
}

-(void)loading{
    webview=[[UIWebView alloc]initWithFrame:CGRectMake(-10, 0, width+10, height)];
    webview.backgroundColor=[UIColor colorWithRed:0.0/255 green:0.0/255 blue:0.0/255 alpha:0.5f];
    webview.opaque=NO;
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *resPath = [bundle resourcePath];
    NSString *filePath = [resPath stringByAppendingPathComponent:@"demo31.html"];
    
    [webview loadHTMLString:[NSString stringWithContentsOfFile:filePath]
                    baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]];
    [self.view addSubview:webview];
    
}




-(void)referTime:(id)sender{
    if(success==0){
        [self checkConnection];
        //[HTFrameAlert hmAlertInfo:@"我是定时器"];
    }
    
}


- (CGRect)borderRectForBounds:(CGRect)bounds

{
    
    bounds.size.height = 44;
    
    return bounds;
    
}





-(void)checkConnection{
    [HTSysLogin hmCheckLogin_ASI:@"HT" andUserName:@"001" andPassword:@"1" complete:^{
        
        success=1;
        
    }finally:^{
        
    }];
}


-(void)eulaAction:(UIButton*)sender{
    
    UITapGestureRecognizer *sub=(UITapGestureRecognizer *)sender;
    NSString *storyboard=@"Eula";
    NSString *Identifier=@"Eula";
    UIStoryboard *valueView = [UIStoryboard storyboardWithName:storyboard bundle:nil];
    UIViewController *uv=[valueView instantiateViewControllerWithIdentifier:Identifier];
    UINavigationController *baNavigation = [[UINavigationController alloc] initWithRootViewController:uv];
    [self presentModalViewController:baNavigation animated:YES];
    
    //返回
    
    
}



//登录
- (void)loginAction:(UIButton*)sender {
    
    if([inUserField.text isEqualToString:@""]){
        [HTFrameAlert hmAlertInfo:@"请输入账号"];
        return;
    }
    
    if([inPasswordField.text isEqualToString:@""]){
        [HTFrameAlert hmAlertInfo:@"请输入密码"];
        return;
    }

    [inPasswordField resignFirstResponder];
    [inUserField resignFirstResponder];
    
    
    
    
    NSString *serverUrl=[[NSString alloc]initWithFormat:@"%@\/%@\/",[HTSysParam erpServer],[HTSysParam erpVirDir]];
    HTFrameNet *htnet=[[HTFrameNet alloc] initWithServerUrl:serverUrl];
    NSDictionary *dict = @{ @"opid":inUserField.text, @"password":inPasswordField.text};
    [htnet hmNetWCFmethodName:@"web/oP/checkLogin" andDic:dict complete:^(NSString *backstr)
     {
         NSDictionary *formdict=[HTFrameNet hmConvertDictFromJsonStr:backstr];//表单单头
         int status=[[formdict objectForKey:@"status"]intValue];
         if(status==0){
             
             NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
             //保存用户名
             [defaults setObject:inUserField.text forKey:@"username"];
             [defaults setObject:inPasswordField.text forKey:@"password"];
             //写入文件，内部自动创建一个plist
             [defaults synchronize];
             [self.loginIndicator startAnimating];
             
             NSDictionary *body=[formdict objectForKey:@"body"];
             AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
             [HTSysParam autoLoginFlagSet:false];//非自动登录参数设置
             //跳转到主页面
             [self performSegueWithIdentifier:@"loginsegue" sender:nil];
         }
         else{
             NSDictionary *error=[formdict objectForKey:@"error"];
             NSString *errorCode=[error objectForKey:@"code"];
             
             if([errorCode isEqualToString:@"110101"]){
                 [HTFrameAlert hmAlertInfo:@"用户名不存在"];
             }
             
             if([errorCode isEqualToString:@"120101"]){
                 [HTFrameAlert hmAlertInfo:@"密码错误"];
             }
             

             
         }
         
     }];
    

    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
